// This file is a template Textobot plugin implementation.
// You can use this file as the new plugin's startup code.
// API Reference: 
// https://gitee.com/geekneo/Textobot/blob/master/Doc/api.md
// 
// If you have any advises, We are happy to hear from you.
// Follow us:
// ----------------------------------------------------------------------
//         Email                           971159199@qq.com
//         公众号                           刘柏江
//         百家号                           刘柏江VM
// 
//         微博                             刘柏江VM
//         头条                             刘柏江VM
//         码云                             https://gitee.com/geekneo/
// ----------------------------------------------------------------------
// 
// 
// Version history:
// 
// 2019.12.14 {
//         * initialize template plugin code
// }

// it's only for VSCode Textobot-Run command
// TODO:
// turn to 0 before you pack this plugin
// turn to 1 if you want to run directly from VSCode
const dev_mode = 0;

// ensure our module is loaded
const libtextobot = Module.load('/Library/MobileSubstrate/DynamicLibraries/textobot.dylib');
function TB_api(name, rettype, argstype) {
    return new NativeFunction(libtextobot.getExportByName(name), rettype, argstype);
}

// pickup textobot APIs
const textobot = {
    TB_logpath      : TB_api('TB_logpath', 'pointer', []),
    TB_logbuff      : TB_api('TB_logbuff', 'pointer', []),
    TB_logflush     : TB_api('TB_logflush', 'int', []),
    TB_dialog       : TB_api('TB_dialog','int', ['pointer']),
    TB_system       : TB_api('TB_system','int', ['pointer']),
    TB_root_system  : TB_api('TB_root_system', 'int', ['pointer']),
    TB_window       : TB_api('TB_window', 'pointer', []),
    TB_touch        : TB_api('TB_touch', 'int', ['int', 'int']),
    TB_swipe        : TB_api('TB_swipe', 'int', ['int', 'int', 'int', 'int']),
    // API likes TB_*_new should call TB_sptr_free to free the heap memory
    // see free function bellow
    TB_appdir_new   : TB_api('TB_appdir_new', 'pointer', []),
    TB_docdir_new   : TB_api('TB_docdir_new', 'pointer', []),
    TB_tmpdir_new   : TB_api('TB_tmpdir_new', 'pointer', []),
    TB_urlget_new   : TB_api('TB_urlget_new', 'pointer', ['pointer']),
    TB_urlpost_new  : TB_api('TB_urlpost_new', 'pointer', ['pointer', 'pointer']),
    TB_sptr_free    : TB_api('TB_sptr_free', 'void', ['pointer']),
    // ...
    // see https://gitee.com/geekneo/Textobot/blob/master/Doc/api.md for more APIs
};

// put the string to textobot's native buffer
function puts(s) {
    // logbuff's size is 4096
    if (s.length >= 4096) {
        console.log('Ignoring long string ' + s.length);
        return;
    }
    textobot.TB_logbuff().writeUtf8String(s);
    return textobot.TB_logbuff();
}

// wrap logger util
function logs(s) {
    puts(s);
    textobot.TB_logflush();
}

// wrap free util
function free(ptr) {
    var sptr = puts(ptr.toString(16));
    textobot.TB_sptr_free(sptr);
}

// slow logger, usually for global log
console.log("The textobot template plugin's log file is at " + 
    textobot.TB_logpath().readUtf8String());

// fast log, usually for hooker with better performance
logs('The textobot template plugin is running ...');

function demo_impl() {
    const appdir = textobot.TB_appdir_new();
    // show a dialog
    // the first line is TITLE
    // the left lines is CONTENT
    textobot.TB_dialog(puts(
        'Textobot Tips' +
        '\n' +
        'The textobot template plugin is running inside ' +
        appdir.readUtf8String()
    ));

    // API ends with _new should call free deallocate the heap memory
    free(appdir);
}

// TODO: add your implementation for directly run mode entry
function dev_entry() {
    demo_impl();
}

// TODO: add your implementation for plugin release mode entry
function plugin_entry() {
    demo_impl();
}

// dev mode is only for VSCode Textobot-Run command
if (dev_mode) {
    dev_entry();
}
// running in release plugin mode
else {
    plugin_entry();
}
